package org.tourism.aop;


import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.tourism.VO.UserVO;
import org.tourism.common.CommonResponse;

import java.io.IOException;

@Aspect
@Component
public class LoginAspect {

    @Before("@annotation(CheckLogin)")
    @ResponseBody
    public CommonResponse checkLogin() throws IOException{
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return CommonResponse.createForError(500, "检查是否登录时的奇怪错误");
        }else{
            HttpServletRequest request = attributes.getRequest();
            UserVO userVO = (UserVO) request.getSession().getAttribute("user");
            if(userVO == null) {
                return CommonResponse.createForError(10, "请先登录");
            }
        }
        return CommonResponse.createForIsLogin("登录验证成功");
    }
}
